<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <include file="./Public/Admin/header.html" />
    <script type="text/javascript" src="__PUBLIC__js/jquery_wookmark_min.js?2013-10-15-2"></script>
    <script type="text/javascript" src="__PUBLIC__js/masonry.pkgd.min.js?2013-10-15-2"></script>
    <script type="text/javascript" src="__PUBLIC__js/jquery_imagesloaded.js?2013-10-15-2"></script>
    <script type="text/javascript" src="__PUBLIC__js/jquery.pagination.js?2013-10-15-2"></script>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__css/emotion.css?2013-10-15-2" media="all" />
    <script type="text/javascript" src="__PUBLIC__js/emotion.js?2013-10-15-2"></script>
    <script type="text/javascript" src="__PUBLIC__js/resource.js?2013-10-15-2"></script>
</head>
<body>

<div id="main">
<div class="container-fluid">

<div class="row-fluid">
<div class="span12">

<div class="box">
<div class="box-title">
    <div class="span10">
        <h3><i class="icon-table"></i>自定义菜单管理  </h3>
    </div>
    <div class="span2"><a class="btn" href="Javascript:window.history.go(-1)">返回</a></div>
</div>

<div class="box-content">
<div class="alert">
    <strong>温馨提示：</strong><br />
    1. 一级菜单个数为2~3个，只有保存了一级菜单后，才可以添加二级菜单，二级子菜单个数为2~5个!<br />
    2. 菜单在微信界面的显示位置根据“显示顺序”排列，如果显示顺序相同，则随机排列！<br />
    3. <strong>生成自定义菜单,必须在已经保存的基础上进行,临时勾选启用点击生成是无效的! 第一步必须先【<span class="red bold">保存</span>】，第二步再点击【<span class="red bold">生成自定义菜单</span>】!</strong>

</div>
<div class="row-fluid">

    <div class="span8 control-group">
        <a href="javascript:void(0)" class="btn" id="add_menu"><i class="icon-plus"></i>添加主菜单</a>
    </div>

</div>

<div class="row-fluid dataTables_wrapper">
<form action="{:U('Admin/Wechat/menu')}" method="post" class="form-horizontal form-validate">
    <table id="listTable" class="table table-bordered table-hover dataTable">

        <thead>
        <tr>
            <th>显示顺序</th>
            <th>主菜单名称</th>
            <th>菜单类型(设置/查看)<!--触发关键词或链接地址--></th>
            <th>启用</th>
            <th>操作</th>
        </tr>
        </thead>

        <?php foreach($mainmenu as $item):?>
        <tr class="ptr">
            <td>
                <input type="text" class="input-mini required" size="3" value="<?php echo $item['sortnum']?>" name="ps[<?php echo $item['menuid']?>][sort]"   data-rule-number="true"/></td>
            <td>
                <input type="text" class="input-medium" size="15" value="<?php echo $item['menuname']?>"  name="ps[<?php echo $item['menuid']?>][name]" data-rule-required="true" data-rule-maxlength="30" />
                <i class="icon-plus cursor_p add" title="添加子菜单" rel="<?php echo $item['menuid']?>"></i>
            </td>
            <td>
                <div class="form-inline hide">
                    <input type="text" class="input-medium type" size="15" value="<?php echo ($item['menutype']=='click')?$item['menukey']:$item['menuurl']?>"  name="ps[<?php echo $item['menuid']?>][key]" data-rule-required="true" data-rule-maxlength="200" />
                    <label class="radio"><input type="radio" name="ps[<?php echo $item['menuid']?>][is_url]"  value="1" <?php echo ($item['menutype']=='click')?'checked':''?>/> 关键字</label>
                    <label class="radio"><input type="radio" name="ps[<?php echo $item['menuid']?>][is_url]"  value="2" <?php echo ($item['menutype']=='view')?'checked':''?>/> 链接</label>

                    <span id="ps[<?php echo $item['menuid']?>][menutype]"><?php echo $item['menutype']=='click'?($item['menukey']==''?'':$item['menutype']):$item['menutype']?></span>
                    <a class="btn btn-mini" attr="menu-edit_<?php echo $item['menuid']?>" data-action="edit" data-menuid="<?php echo $item['menuid']?>" data-menutype="<?php echo $item['menutype']?>" data-menukey="<?php echo $item['menukey']?>" data-menuurl="<?php echo $item['menuurl']?>" title="设置"><i class="icon-cog"></i></a>
                    <a class="btn btn-mini " attr="menu-view_<?php echo $item['menuid']?>" data-action="edit" data-menuid="<?php echo $item['menuid']?>" data-menutype="<?php echo $item['menutype']?>" data-menukey="<?php echo $item['menukey']?>" data-menuurl="<?php echo $item['menuurl']?>" title="查看"><i class="icon-search"></i></a></div>
                <span class="label <?php echo $item['menutype']=='click'?($item['menukey']==''?'label-important':'label-info'):'label-success'?>" id="ps[<?php echo $item['menuid']?>][menutype]"><?php echo $item['menutype']=='click'?($item['menukey']==''?'未设置':$item['menutype']):$item['menutype']?></span>
                <a class="btn btn-mini" attr="menu-edit_<?php echo $item['menuid']?>" data-action="edit" data-menuid="<?php echo $item['menuid']?>" data-menutype="<?php echo $item['menutype']?>" data-menukey="<?php echo $item['menukey']?>" data-menuurl="<?php echo $item['menuurl']?>" title="设置"><i class="icon-cog"></i></a>
                <a class="btn btn-mini" attr="menu-view_<?php echo $item['menuid']?>" data-action="edit" data-menuid="<?php echo $item['menuid']?>" data-menutype="<?php echo $item['menutype']=='click'?($item['menukey']==''?'':$item['menutype']):$item['menutype']?>" data-menukey="<?php echo $item['menukey']?>" data-menuurl="<?php echo $item['menuurl']?>" title="查看"><i class="icon-search"></i></a>

                <input type="hidden" value="<?php echo $item['parentmenuid']?>" name="ps[<?php echo $item['menuid']?>][parent_id]"/>
                <input type="hidden" class="key_type" value="1" name="ps[<?php echo $item['menuid']?>][type]">
            </td>
            <td>
                <input attr="check-menu_<?php echo $item['menuid']?>" type="checkbox" name="ps[<?php echo $item['menuid']?>][is_show]" value="1" <?php echo ($item['isshow']=='1')?'checked="checked"':''?>/></td>
            <td><a href="javascript:G.ui.tips.confirm('您确定要删除此菜单吗?', '{:U('Admin/Wechat/delmenu')}?menuid=<?php echo $item['menuid']?>');">删除</a></td>
        </tr>
        <?php $submenu=get_mainmenu($menulist,$item['menuid']);
        if(count($submenu)>0){
        foreach($submenu as $subitem){
        ?>
        <tr class="ztr">
            <td>
                <input type="text" class="input-mini required" size="3" value="<?php echo $subitem['sortnum']?>" name="ps[<?php echo $subitem['menuid']?>][sort]"   data-rule-number="true"/></td>
            <td>
                <i class='board'></i>
                <input type="text" class="input-medium" size="15" value="<?php echo $subitem['menuname']?>"  name="ps[<?php echo $subitem['menuid']?>][name]" data-rule-required="true" data-rule-maxlength="30" />

            </td>
            <td >
                <div class="form-inline hide"> <input type="text" class="input-medium type" size="15" value="<?php echo ($subitem['menutype']=='click')?$subitem['menukey']:$subitem['menuurl']?>"  name="ps[<?php echo $subitem['menuid']?>][key]" data-rule-required="true" data-rule-maxlength="200" />
                    <label class="radio"><input type="radio" name="ps[<?php echo $subitem['menuid']?>][is_url]"  value="1" <?php echo ($subitem['menutype']=='click')?'checked':''?>/> 关键字</label>
                    <label class="radio"><input type="radio" name="ps[<?php echo $subitem['menuid']?>][is_url]"  value="2" <?php echo ($subitem['menutype']=='view')?'checked':''?>/> 链接</label>
                </div>

                <span class="label <?php echo $subitem['menutype']=='click'?($subitem['menukey']==''?'label-important':'label-info'):'label-success'?>" id="ps[<?php echo $subitem['menuid']?>][menutype]"><?php echo $subitem['menutype']=='click'?($subitem['menukey']==''?'未设置':$subitem['menutype']):$subitem['menutype']?></span>
                <a class="btn btn-mini" attr="menu-edit_<?php echo $subitem['menuid']?>" data-action="edit" data-menuid="<?php echo $subitem['menuid']?>" data-menutype="<?php echo $subitem['menutype']?>" data-menukey="<?php echo $subitem['menukey']?>" data-menuurl="<?php echo $subitem['menuurl']?>" title="设置"><i class="icon-cog"></i></a>
                <a class="btn btn-mini" attr="menu-view_<?php echo $subitem['menuid']?>" data-action="edit" data-menuid="<?php echo $subitem['menuid']?>" data-menutype="<?php echo $subitem['menutype']=='click'?($subitem['menukey']==''?'':$subitem['menutype']):$subitem['menutype']?>" data-menukey="<?php echo $subitem['menukey']?>" data-menuurl="<?php echo $subitem['menuurl']?>" title="查看"><i class="icon-search"></i></a>
                <input type="hidden" value="<?php echo $subitem['parentmenuid']?>" name="ps[<?php echo $subitem['menuid']?>][parent_id]"/>
                <input type="hidden" class="key_type" value="1" name="ps[<?php echo $subitem['menuid']?>][type]">
            </td>
            <td >
                <input type="checkbox" attr="z-check-menu_<?php echo $subitem['parentmenuid']?>_<?php echo $subitem['menuid']?>" name="ps[<?php echo $subitem['menuid']?>][is_show]" value="1" <?php echo ($subitem['isshow']=='1')?'checked="checked"':''?>/></td>
            <td><a href="javascript:G.ui.tips.confirm('您确定要删除此菜单吗?', '{:U('Admin/Wechat/delmenu')}?menuid=<?php echo $subitem['menuid']?>');">删除</a></td>
        </tr>

        <?php
                                                }
                                            }
                                        ?>
        <?php endforeach;?>
    </table>
    <div>
        <button id="bsubmit" type="submit" data-loading-text="提交中..." class="btn btn-primary">保存</button>

        <button id="create_menu"  style="margin-left: 80px;" type="button"  class="btn btn-primary">生成自定义菜单</button>


    </div>
</form>


<script type="text/javascript">
    $().ready(function () {
        var $add_menu = $("#add_menu");
        var $add_zmenu = $("i.add");
        var $menu_index = 0;


        $add_menu.click(function (e) {
            e.preventDefault();
            $menu_index++;
            var _menuPtrtmp = '<tr>'
                    + ' <td>'
                    + '  <input name="new[sort][' + $menu_index + ']" size="3" type="text" value="0" class="input-mini" data-rule-required="true" data-rule-number="true" /></td>'
                    + ' <td>'
                    + '<input name="new[name][' + $menu_index + ']" size="15" type="text" class="input-medium" data-rule-required="true" data-rule-maxlength="30" /></td>'
                    + '<td>'
                    + ' <div class="form-inline hide"><input name="new[key][' + $menu_index + ']" size="15" type="text" class="input-medium type" data-rule-required="true" data-rule-maxlength="500" />'
                    +'   <label class="radio"><input type="radio" name="new[is_url][' + $menu_index + ']"  value="1" checked/> 关键字</label>'
                    +'   <label class="radio"><input type="radio" name="new[is_url][' + $menu_index + ']"  value="2" /> 链接</label>'
                    +'<span id="new[menutype]['+$menu_index+']"></span>'
                    +'<a class="btn btn-mini" attr="menu-edit_'+$menu_index+'" data-action="add" data-menuid="'+$menu_index+'" data-menutype="" data-menukey="" data-menuurl="" title="设置"><i class="icon-cog"></i></a>'
                    +'<a class="btn btn-mini " attr="menu-view_'+$menu_index+'" data-action="add" data-menuid="'+$menu_index+'" data-menutype="" data-menukey="" data-menuurl="" title="查看"><i class="icon-search"></i></a></div>'
                    + '  <input type="hidden" name="new[parent_id][' + $menu_index + ']" value="{pid}" />'
                    + '  <input type="hidden" name="new[type][' + $menu_index + ']" class="key_type" value="1" /></td>'
                    + ' <td>'
                    + '  <input type="checkbox" name="new[is_show][' + $menu_index + ']" checked="checked" value="1"/></td>'
                    + ' <td><a href="javascript:void()" class="del">删除</a></td>'
                    + '</tr> ';
            $("#listTable").append(_menuPtrtmp.replace("{pid}", 0));

        })
        $add_zmenu.click(function myfunction() {
            var $pid = $(this).attr("rel");
            var $thistr = $(this).parent().parent();
            var next = $thistr.nextAll("tr");
            $menu_index++;
            var _menuPtrtmp = '<tr>'
                    + ' <td>'
                    + '  <input name="new[sort][' + $menu_index + ']" size="3" type="text" value="0" class="input-mini" data-rule-required="true" data-rule-number="true" /></td>'
                    + ' <td>{z}'
                    + '<input name="new[name][' + $menu_index + ']" size="15" type="text" class="input-medium" data-rule-required="true" data-rule-maxlength="30" /></td>'
                        /*+ '<td>'
                         + ' <input name="new[key][' + $menu_index + ']" size="15" type="text" class="input-medium type" data-rule-required="true" data-rule-maxlength="500" />'
                         + '  <input type="hidden" name="new[parent_id][' + $menu_index + ']" value="{pid}" />'
                         + '  <input type="hidden" name="new[type][' + $menu_index + ']" class="key_type" value="1" /></td>'
                         + ' <td>'*/
                    + '<td>'
                    + ' <div class="form-inline hide"><input name="new[key][' + $menu_index + ']" size="15" type="text" class="input-medium type" data-rule-required="true" data-rule-maxlength="500" />'
                    +'   <label class="radio"><input type="radio" name="new[is_url][' + $menu_index + ']"  value="1" checked/> 关键字</label>'
                    +'   <label class="radio"><input type="radio" name="new[is_url][' + $menu_index + ']"  value="2" /> 链接</label></div> '
                    +'<span class="label label-important" id="new[menutype]['+$menu_index+']">未设置</span>'
                    +'<a class="btn btn-mini" attr="menu-edit_'+$menu_index+'" data-action="add" data-menuid="'+$menu_index+'" data-menutype="" data-menukey="" data-menuurl="" title="设置"><i class="icon-cog"></i></a>'
                    +'<a class="btn btn-mini " attr="menu-view_'+$menu_index+'" data-action="add" data-menuid="'+$menu_index+'" data-menutype="" data-menukey="" data-menuurl="" title="查看"><i class="icon-search"></i></a>'
                    + '  <input type="hidden" name="new[parent_id][' + $menu_index + ']" value="{pid}" />'
                    + '  <input type="hidden" name="new[type][' + $menu_index + ']" class="key_type" value="1" /></td>'
                    + ' <td>'
                    + '  <input type="checkbox" attr="z-check-menu_'+$pid+'_' + $menu_index + '" name="new[is_show][' + $menu_index + ']" checked="checked" value="1" /></td>'
                    + ' <td><a href="javascript:void()" class="del">删除</a></td>'
                    + '</tr> ';

            var tp = _menuPtrtmp.replace("{pid}", $pid).replace("{z}", "<i class='board'></i>  ");
            if (next.length > 0) {
                next.first().before(tp);

            } else {
                $("#listTable").append(tp);
            }


        });
        $("#listTable .del").live("click", (function () {
            $(this).parents("tr").remove();
        }));
        $("input.type").live("change", function () {
            var $this = $(this);
            var $val = $this.val();
            var $nex = $this.nextAll("input.key_type");
            var re = /^((http|https|ftp):\/\/)?(\w(\:\w)?@)?([0-9a-z_-]+\.)*?([a-z0-9-]+\.[a-z]{2,6}(\.[a-z]{2})?(\:[0-9]{2,6})?)((\/[^?#<>\/\\*":]*)+(\?[^#]*)?(#.*)?)?$/i;
            if (re.test($val)) { $nex.val(2) } else { $nex.val(1) };
        });
        //生成菜单
        $("#create_menu").click(function () {

            var $idsCheck = $("#listTable :checkbox");
            var $isnew = false;
            $idsCheck.each(function () {
                var $hidden_name = $(this).parents("tr").find("input[type=hidden]").attr("name");
                if ($hidden_name.indexOf("new") >= 0) $isnew = true; return;
            });
            if ($isnew) {
                G.ui.tips.info("当前页面存在有保存菜单 请保存后生成!")
            } else {
                var $p = 0;
                var $z = 0;
                var $ftr = $("#listTable .ptr");

                $ftr.each(function (k, v) {
                    if ($p > 3) return false;
                    if ($z > 5) return false;
                    $z = 0;
                    var $this = $(this);
                    if ($this.find("input[type='checkbox']:checked").length > 0) {
                        $p++;

                        $this.nextUntil(".ptr").each(function () {
                            if ($(this).find("input[type='checkbox']:checked").length > 0) {
                                $z++;
                            }
                        });
                        if ($z == 0 && k == $ftr.length) {
                            $this.nextAll(".ztr").each(function () {
                                if ($(this).find("input[type='checkbox']:checked").length > 0) {
                                    $z++;
                                }
                            });
                        }
                    }

                });
                //if ($z<2||$z > 5) { G.ui.tips.info("2级菜单数量为2个至5个"); return false };
                //if ($p<2||$p > 3) { G.ui.tips.info("1级菜单数量为2个至3个"); return false };
                //检测是否有未设置项
                /*
                 var isset=false;
                 //$("#listTable .ztr").removeClass('error');
                 $("span[id^='ps['][id$='][menutype]']").each(function(){
                 var val=$(this).html();
                 if(val=='未设置'){
                 $(this).parent().parent().addClass('error');
                 isset=true;
                 }
                 })
                 if(isset){alert("有菜单未设置！");return false;}
                 $("#listTable .ztr").each(function(){
                 if($(this).hasClass('error')){
                 alert('请先保存菜单！');
                 return false;
                 }
                 })
                 */
                $("#create_menu").text('正在生成菜单...').addClass('disabled');
                //alert('生成菜单');return false;
                $.get("{:U('Admin/Wechat/createmenu')}", {}, function (data) {
                    $("#create_menu").text('生成自定义菜单').removeClass('disabled');
                    G.ui.tips.info(data.error);
                }, 'json');
            }
        });

        $("input[attr^='check-menu_']").live('change',function(){
            var tmp = $(this).attr('attr').split('_');
            var mid=tmp[1];
            if($(this).attr("checked")){
                //alert('checked');z-check-menu_
                //alert($("input[attr^='z-check-menu_"+mid+"_']").length);
                $("input[attr^='z-check-menu_"+mid+"_']").attr("checked",true);
            }else{
                $("input[attr^='z-check-menu_"+mid+"_']").attr("checked",false);
                //alert('error')
            }

        })

        $("input[attr^='z-check-menu_']").live('click',function(){
            var tmp=$(this).attr('attr').split('_');
            var mid=tmp[1];
            var len=$("input[attr^='z-check-menu_"+mid+"_']").length;
            var b=$("input[attr^='z-check-menu_"+mid+"_']").filter(":checked").length==len;
            //alert(b+'');
            if(b){
                var flag=$("input[attr='check-menu_"+mid+"']").prop("checked",b?true:false);
            }
            //var flag=$("input[attr='check-menu_"+mid+"']").prop("checked",b?true:false);
        })

    });
</script>

</div>
</div>
</div>
</div>

</div>
</div>
</div>

<script type="text/javascript">
    //全局变量
    var action='';//设置菜单内容时的类型
    var menuid='';//设置菜单内容时的id
    $(document).ready(function(){
        //设置
        $("a[attr^='menu-edit_']").live('click',function(){
            action=$(this).attr('data-action');
            menuid=$(this).attr('data-menuid');
            var menutype=$(this).attr('data-menutype');
            selcontents();
            $('#txt_menuurl').val('');
            if(menutype=='view'){
                $('#txt_menuurl').val($(this).attr('data-menuurl'));
                $('#myTab a:last').tab('show');
            }else{
                $('#myTab a:first').tab('show');
            }
            if(action=='add'){
                //$("span[id='new[menutype]["+menuid+"]']").html(menuid);
                //$("input[type='radio'][name='new[is_url]["+menuid+"]'][value='2']").attr("checked",true);
            }
        });
        //查看
        $("a[attr^='menu-view_']").live('click',function(){
            var action=$(this).attr('data-action');
            var menutype=$(this).attr('data-menutype');
            var menukey=$(this).attr('data-menukey');
            var menuurl=$(this).attr('data-menuurl');
            if(menutype==''){
                alert('菜单未设置！');
                return false;
            }
            viewReply(menutype,menukey,menuurl);

            //alert(menutype);
        });

    })
</script>

<!--设置-->
<style type="text/css">
    #myModal {
        /* SET THE WIDTH OF THE MODAL 设置Modal的宽度*/
        width: 900px;
        /* CHANGE MARGINS TO ACCOMODATE THE NEW WIDTH (original = margin: -250px 0 0 -280px;) */
        margin: 0px 0 0 -450px;
    }
    .con-masonry {
        list-style-type: none;
        margin: 0 auto;
    }
    .con-masonry .item_news {
        width: 350px;
        margin: 5px;
    }
</style>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-header">
    <button class="close" type="button" data-dismiss="modal">×</button>    <h3>设置内容</h3>
</div>
<div class="modal-body">
    <iframe id='iframe01' style="zoom: 0.6;overflow-x:hidden; display:none;" src="" frameBorder="0" marginheight="0" marginwidth="0" width="99.8%" height="600"></iframe>
    <ul class="nav nav-tabs" id="myTab">
        <li><a href="#replytext" data-toggle="tab" data-type="text">文本素材</a></li>
        <li><a href="#replynews" data-toggle="tab" data-type="news">图文素材</a></li>
        <li><a href="#menulink" data-toggle="tab" data-type="link">链接</a></li>
    </ul>
    <div class="tab-content dataTables_wrapper">
        <div class="tab-pane  active" id="replytext">
            <div class="con-masonry"  >

            </div>
        </div>
        <div class="tab-pane" id="replynews">
            <div class="con-masonry"  >


            </div>
        </div>
        <div  class="dataTables_paginate paging_full_numbers " id="pagination"></div>
        <div class="tab-pane" id="menulink">

            <div class="con-masonry1 form-horizontal"  >
                <div class="control-group">
                    <label for="txt_menuurl" class="control-label hide" style=""></label>
                    <div class="controls1" style="">
                        <strong>链接地址(url)：</strong> <input type="text" name="txt_menuurl" id="txt_menuurl" class=" input-large"  style="width: 400px;" value=""><span class="maroon">*</span><br />
                        <span style="margin-left: 90px;">链接地址时请填写以"http://"开头，这样可以保证用户手机浏览的兼容性更好</span>
                    </div>
                </div>
                <div class="form-actions1" style="">
                    <button class="btn btn-primary" style="margin-left: 90px;" id="btn-menuset-url">确定</button>
                </div>
                <br /><br />

            </div>
        </div>
    </div>

</div>
<div class="modal-footer hide">
    <button class="btn btn-primary" data-dismiss="modal">确定</button>
</div>
<script type="text/javascript">
    Emotion.url='/thinkphp/Public/Admin/img/face/';
    $(function () {
        $('.des').each(function(){
            var c=$(this).html();
            c=Emotion.replaceEmoji(c);
            c=c.replace(/&lt;/gi, "<").replace(/&gt;/gi, ">");
            $(this).html(c);
        })
    })
    var type='text';
    var isinitpage=true;
    $(function () {

        $('a[data-toggle="tab"]').on('shown', function (e) {
            type=$(this).attr('data-type');
            if(type!='link'){
                isinitpage=true;
                get_contents(type,0);
                $('#pagination').show();
            }else{
                $('#pagination').hide();
            }

            //e.target;
            //alert(e.target); // activated tab
            //e.relatedTarget; // previous tab
        })

        $('#myModal').on('shown', function () {
            // do something…
            if(type!='link'){
                get_contents(type,0);
                $('#pagination').show();
            }else{
                $('#pagination').hide();
            }
            //get_contents(type,0);
            //getcontent();
        })

    })
    function selcontents(){
        $('#myModal').modal('show');
    }
    function getcontent(){
        $('.con-masonry').masonry({
            // options
            itemSelector : '.item_news',
            columnWidth : 390,//一列的宽度 Integer
            isFitWidth: true,// 适应宽度
            gutterWidth :10,//列的间隙
            isResizableL:true, // 是否可调整大小
            transitionDuration:0,//过渡时间
            isRTL:false//使用从右到左的布局
        });
    }
</script>
<script type="text/javascript">
    //加载数据
    function get_contents(type,page){
        page=page+1;
        var opts={type:type,page:page};
        $.get("{:U('Admin/Wechat/selcontent')}?t="+new Date().getTime(),opts,function(data){

            if(isinitpage){
                initPagination(data.totlecount);
                isinitpage=false;
            }
            //alert(data.totlecount);
            //载入数据
            if(type=='news'){
                $('#replynews .con-masonry').empty();
                $('#replynews .con-masonry').masonry('reload');
                if(data.totlecount==0){
                    $('#replynews .con-masonry').append('没有图文素材');
                }
            }else{
                $('#replytext .con-masonry').empty();
                $('#replytext .con-masonry').masonry('reload');
                if(data.totlecount==0){
                    $('#replytext .con-masonry').append('没有文本素材');
                }
            }
            $.each(data.list,function(index,item){
                var str='';
                if(type=='news'){
                    var conitem=eval("("+item.content+")");//内容转换为json对象
                    if(conitem.length==1){
                    //单图文
                        str+='<div class="item_news">'
                        +'<div class="imgEdit " id="imgEdit">'
                        +' <div  id="item_0">'
                        +'  <div class="caption div_caption" >'+conitem[0].detailtitle+' </div>'
                        +' <div class="time">'+item.addtime+'</div>'
                        +'<div class="img"><div class="coverImg">'
                        +' <img  src="'+conitem[0].detaillogo+'" />'
                        +'</div></div>'
                        +'<div class="des">'+conitem[0].detailintro+'</div></div>'
                        +'<div class="clearfix">'
                        +'<a class="btn btn-primary selcontent" data-cid="'+item.contentid+'" style="width:300px" title="选择此素材"><i class="icon-ok"></i></a>'
                        +' </div></div></div>';
                    }else{
                    //多图文
                        str+='<div class="item_news">'
                        +' <div class="imgsEdit " id="imgsEdit">'
                        +'                     <div class="time">'+item.addtime+'</div>'
                        +'                    <div class="img" id="item_0">'
                        +'                        <div class="coverImg"><img src="'+conitem[0].detaillogo+'"/></div>'
                        +'                        <div class="caption" style=" ">'
                        +'                            <span  class="div_caption" >'+conitem[0].detailtitle+' </span>'
                        +'                       </div>'
                        +'                   </div>'
                        +'                   <ul class="unstyled">';
                        for(var i=1;i<conitem.length;i++){
                            str+='    <li class="item">'
                            +'       <div class="caption div_caption">'+conitem[i].detailtitle+' </div>'
                            +'     <div class="itemImg coverImg"><img src="'+conitem[i].detaillogo+'"/></div>'
                            +'     <div class="div_content hide"></div>'
                            +' </li>';
                        }
                        str+='     </ul> ';
                        str+='<div class="clearfix">'
                        +'<a class="btn btn-primary selcontent" data-cid="'+item.contentid+'"  style="width:300px" title="选择此素材"><i class="icon-ok"></i></a>'
                        +' </div></div>  </div>';
                    }
                    var $boxes =$(str);
                    $('#replynews .con-masonry').append($boxes).masonry('appended', $boxes, true);
                }else{
                    var c=Emotion.replaceEmoji(item.content);
                    c=c.replace(/&lt;/gi, "<").replace(/&gt;/gi, ">");
                    str+='<div class="item_news">'
                    +'                <div class="imgEdit" id="imgEdit">'
                    +'                        <div  id="item_0">'
                    +'                            <div class="time">'+item.addtime+'</div>'
                    +'                            <div class="des" style="">'+c+'</div>   '
                    +' </div> '
                    +'<br /><div class="clearfix">'
                    +'<a class="btn btn-primary selcontent" data-cid="'+item.contentid+'"  style="width:300px" title="选择此素材"><i class="icon-ok"></i></a>'
                    +' </div></div>  </div>';
                    var $boxes =$(str);
                    $('#replytext .con-masonry').append($boxes).masonry('appended', $boxes, true);
                }
            });
            getcontent();
        },'json');

    }

    function initPagination(num_entries){
        //渲染分页
        $('#pagination').pagination(num_entries, {
            current_page : 0,
            items_per_page : 10,
            num_display_entries : 3,
            callback : function(page_id){
                //模拟ajax去后端读取页数，获取数据并渲染列表的过程
                /*$('#list-content').html('');
                 for(var i = 0; i < 10; i++){
                 $('#list-content').append('<tr><td>' + arr[page_id * 10 + i].number + '</td><td>' + arr[page_id * 10 + i]._date + '</td></tr>');
                 }*/
                get_contents(type,page_id);
            },
            prev_text : '上一页',
            next_text : '下一页'
        });
    }
    $(function () {
        //选择素材确定
        $('.selcontent').live('click',function(){
            var cid=$(this).attr('data-cid');
            var rtype='';
            //alert(type);
            var now_type='';
            if(type=='link'){
                now_type='view';
            }else{
                now_type='click';
            }
            if(action=='add'){
                $("input[name='new[key]["+menuid+"]']").val(cid);
                $("span[id='new[menutype]["+menuid+"]']").html(now_type).removeClass().addClass('label label-info');
                $("input[type='radio'][name='new[is_url]["+menuid+"]'][value='1']").attr("checked",true);
            }else if(action=='edit'){
                $("input[name='ps["+menuid+"][key]']").val(cid);
                $("span[id='ps["+menuid+"][menutype]']").html(now_type).removeClass().addClass('label label-info');
                $("input[type='radio'][name='ps["+menuid+"][is_url]'][value='1']").attr("checked",true);
            }
            $("a[attr='menu-edit_"+menuid+"']").attr('data-menutype',now_type).attr('data-menukey',cid).attr('data-menuurl','');
            $("a[attr='menu-view_"+menuid+"']").attr('data-menutype',now_type).attr('data-menukey',cid).attr('data-menuurl','');
            $('#myModal').modal('hide');
            //$("a[attr='menu-edit_"+menuid+"']").parent().parent().addClass('error');
        })
        //输入链接确定
        $('#btn-menuset-url').live('click',function(){
            var linkurl=$('#txt_menuurl').val();
            $('#txt_menuurl').removeClass('error');
            if(linkurl==''){
                //alert('请输入链接地址！');
                $('#txt_menuurl').addClass('error');
                $('#txt_menuurl').focus();
                return false;
            }
            var now_type='view';
            if(action=='add'){
                $("input[name='new[key]["+menuid+"]']").val(linkurl);
                $("span[id='new[menutype]["+menuid+"]']").html(now_type).removeClass().addClass('label label-success');
                $("input[type='radio'][name='new[is_url]["+menuid+"]'][value='2']").attr("checked",true);
            }else if(action=='edit'){
                $("input[name='ps["+menuid+"][key]']").val(linkurl);
                $("span[id='ps["+menuid+"][menutype]']").html(now_type).removeClass().addClass('label label-success');
                $("input[type='radio'][name='ps["+menuid+"][is_url]'][value='2']").attr("checked",true);
            }
            $("a[attr='menu-edit_"+menuid+"']").attr('data-menutype',now_type).attr('data-menukey','').attr('data-menuurl',linkurl);
            $("a[attr='menu-view_"+menuid+"']").attr('data-menutype',now_type).attr('data-menukey','').attr('data-menuurl',linkurl);
            $('#myModal').modal('hide');
        })


    })
</script>
</div>
<!--设置-->

<!--查看-->
<div id="myModal-view" style="width: 380px;" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"><i class="icon-search"></i> 查看内容</h4>
            </div>
            <div class="modal-body">
                <div class="row-fluid" id="content-panel">

                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary " id="btn-setmanage" data-dismiss="modal">确定</button>
            </div>


        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<script type="text/javascript">

    function viewReply(rtype,cid,url){
        $('#content-panel').html('未查到素材！');
        if(rtype=='view'){
            $('#myModal-view').modal('show');
            $('#content-panel').html('<a href="'+url+'" target="_blank">'+url+'</a>');
            return false;
        }else{
            rtype='textkey';
        }
        //var rtype='textkey';
        $.get("{:U('Admin/Wechat/getcontent')}",{type:rtype,cid:cid},function(data){
            $('#myModal-view').modal('show');
            var str='';
            var item=data;
            if(data.contenttype=='news'){
                var conitem=eval("("+item.content+")");//内容转换为json对象
                if(conitem.length==1){
                //单图文
                    str+='<div class="item_news">'
                    +'<div class="imgEdit " id="imgEdit">'
                    +' <div  id="item_0">'
                    +'  <div class="caption div_caption" >'+conitem[0].detailtitle+' </div>'
                    +' <div class="time">'+item.addtime+'</div>'
                    +'<div class="img"><div class="coverImg">'
                    +' <img  src="'+conitem[0].detaillogo+'" />'
                    +'</div></div>'
                    +'<div class="des">'+conitem[0].detailintro+'</div></div>'
                    +' </div></div>';
                }else{
                //多图文
                    str+='<div class="item_news">'
                    +' <div class="imgsEdit " id="imgsEdit">'
                    +'                     <div class="time">'+item.addtime+'</div>'
                    +'                    <div class="img" id="item_0">'
                    +'                        <div class="coverImg"><img src="'+conitem[0].detaillogo+'"/></div>'
                    +'                        <div class="caption" style=" ">'
                    +'                            <span  class="div_caption" >'+conitem[0].detailtitle+' </span>'
                    +'                       </div>'
                    +'                   </div>'
                    +'                   <ul class="unstyled">';
                    for(var i=1;i<conitem.length;i++){
                        str+='    <li class="item">'
                        +'       <div class="caption div_caption">'+conitem[i].detailtitle+' </div>'
                        +'     <div class="itemImg coverImg"><img src="'+conitem[i].detaillogo+'"/></div>'
                        +'     <div class="div_content hide"></div>'
                        +' </li>';
                    }
                    str+='     </ul> ';
                    str+=' </div>  </div>';
                }
            }else{
                var c=Emotion.replaceEmoji(item.content);
                c=c.replace(/&lt;/gi, "<").replace(/&gt;/gi, ">");
                str+='<div class="item_news">'
                +'                <div class="imgEdit" id="imgEdit">'
                +'                        <div  id="item_0">'
                +'                            <div class="time">'+item.addtime+'</div>'
                +'                            <div class="des" style="">'+c+'</div>   '
                +' </div> '
                +' </div>  </div>';
            }
            str+='<br />';
            $('#content-panel').html(str);
        },'json');
    }

</script>
<!--查看-->
</body>
</html>
